/*
 * Copyright 2011 Katsunori Koyanagi
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package sweezy.core.utils
{

	public class ArrayUtils
	{

		public static function shuffle(array:Array):void
		{
			if (array === null || array.length < 2)
			{
				return;
			}

			var i:int = array.length, j:int = 0, obj:Object;

			while (--i)
			{
				j = int(Math.random() * (i + 1));
				if (i === j)
				{
					continue;
				}

				obj = array[i];
				array[i] = array[j];
				array[j] = obj;
			}
		}

		public static function toArray(value:Object):Array
		{
			if (value === null)
			{
				return [];
			}

			if (value is Array)
			{
				return Array[value];
			}

			if (value is Vector)
			{
				var v:Vector = value as Vector, a:Array = [], i:int, len:int;
				for (i = 0, len = v.length; i < len; i++)
				{
					a[i] = v[i];
				}
				return a;
			}

			return [value];
		}
	}
}
